#!/bin/bash


if [ -z "$1" -o ! -d "$1" ];then
        echo '[err] need arg1 , and arg1 == dir'
        exit
fi

# setting
HOUR=3


####################
# name="tvbox_iptv"
src="$(dirname "$0")"
name="$(basename "$(cd "$src";pwd)")"

pwd


echo "[i] name=$name"

###############
dst_dir="$1"
dst="$dst_dir/$name"


df -h "$dst_dir"

read -t 1 -p "continue in 5 sec, press ctr+c if need exit "
echo 
read -t 5 -p "continue in 5 sec, press ctr+c if need exit "
echo

mkdir "$dst"

if [ ! -d "$dst" ];then
	echo "[E] dir not exits : $dst"
	exit 1
fi

n="cron_job.sh"
echo "$src/$n"

echo  "[i] install to $dst"

cp "$src/$n" "$dst/"
cp "$src/"_* "$dst/"
ls -lF "$dst"

# todo /etc/cron/root
cron_cmd="bash '$dst/$n'"
if ! grep -F "$cron_cmd" /etc/crontab;then 
  echo "[i] append to crontab"

  {
  cat <<EOF
# 0 0 * * * $USER bash xx
10 $HOUR * * * $USER $cron_cmd

EOF

} | sudo tee -a /etc/crontab

fi


bash "$dst/$n" clone


echo "$dst"


